package com.speech.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.speech.R;
import com.speech.item.MissionSuperListViewItem;

import java.util.List;

public class MissionSuperListViewAdapter extends BaseAdapter {

    private List<MissionSuperListViewItem> mLstData;
    private Context mContext;

    private static int index_done = 0;

    public MissionSuperListViewAdapter(List<MissionSuperListViewItem> mLstData, Context mContext) {
        this.mLstData = mLstData;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mLstData.size();
    }

    @Override
    public Object getItem(int position) {
        return mLstData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.item_mission_super, null);

            viewHolder = new ViewHolder();
            viewHolder.tv_target_1 = convertView.findViewById(R.id.tv_item_missionSuper_title_targetNum);
            viewHolder.tv_target_2 = convertView.findViewById(R.id.tv_item_missionSuper_progress_target);
            viewHolder.tv_current = convertView.findViewById(R.id.tv_item_missionSuper_progress_current);
            viewHolder.tv_award = convertView.findViewById(R.id.tv_item_missionSuper_award);

            viewHolder.tv_top = convertView.findViewById(R.id.tv_mission_super_line_top);
            viewHolder.tv_bottom = convertView.findViewById(R.id.tv_mission_super_line_bottom);
            viewHolder.img_left_icon = convertView.findViewById(R.id.img_mission_super_left_icon);

            viewHolder.img_state = convertView.findViewById(R.id.img_item_missionSuper_state);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        MissionSuperListViewItem item = mLstData.get(position);

        viewHolder.tv_target_1.setText(item.iTarget + "");
        viewHolder.tv_target_2.setText(item.iTarget + "");
        viewHolder.tv_current.setText(item.iCurrent + "");
        viewHolder.tv_award.setText(item.strAward);

        if (item.iState == MissionSuperListViewItem.STATE_DONE) {
            if (position == 0) {
                viewHolder.tv_top.setVisibility(View.INVISIBLE);
            } else if (position == mLstData.size() - 1) {
                viewHolder.tv_bottom.setVisibility(View.INVISIBLE);
            }
            index_done = position;
            viewHolder.img_left_icon.setImageResource(R.drawable.mission_super_o);
            viewHolder.img_state.setImageResource(R.drawable.mission_new_finishbutton);
        } else if (item.iState == MissionSuperListViewItem.STATE_GO_FINISH) {
            if (position == index_done + 1) {
                viewHolder.tv_bottom.setVisibility(View.INVISIBLE);
            } else {
                viewHolder.tv_top.setVisibility(View.INVISIBLE);
                viewHolder.tv_bottom.setVisibility(View.INVISIBLE);
            }
            viewHolder.img_state.setImageResource(R.drawable.btn_go_to_finish);
        } else {
            //不显示就不添加了
            viewHolder.tv_top.setVisibility(View.INVISIBLE);
            viewHolder.tv_bottom.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    class ViewHolder {
        TextView tv_target_1;
        TextView tv_target_2;
        TextView tv_current;
        TextView tv_award;
        ImageView img_state;

        TextView tv_top;
        TextView tv_bottom;
        ImageView img_left_icon;
    }
}
